home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / icon tools / iconmanager / whatis / 2.0 / addicon.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  7KB  |  236 lines

  1. /*
  2.  *    AddIcon.c - Copyright © 1991 by S.R. & P.C.
  3.  *
  4.  *    Created:    23 May 1992  12:00:40
  5.  *    Modified:    28 Jul 1992  08:56:19
  6.  *
  7.  *    Make>> cc -qv -qf -pl -sb -wu -wd -wp -wr -wc -hi aztec:include/x.dmp -so <file>.c
  8.  *    Make>> ln -g <file>.o -lstartup -lstring
  9.  */
  10.  
  11. #include "WhatIsBase.h"
  12. #include "string_lib.h"
  13.  
  14. extern struct ExecBase *SysBase;
  15. extern struct DOSLibrary *DOSBase;
  16.  
  17. struct WhatIsBase *WhatIsBase;
  18. struct Library *IconBase;
  19.  
  20. #define MAXFULLNAMELEN 255
  21.  
  22. char *Template = "FileName,ONLY/K,FILES/S,DIRS/S,QUIET/S,FLOAT/S,OVERRIDE/S,ALL/S";
  23.  
  24. #define ARG_FileName 0
  25. #define ARG_ONLY        1
  26. #define ARG_FILES        2
  27. #define ARG_DIRS        3
  28. #define ARG_QUIET        4
  29. #define ARG_FLOAT        5
  30. #define ARG_OVERRIDE    6
  31. #define ARG_ALL        7
  32. #define ARG_ENDARG    8
  33. char *CliHelp = "AddIcon V1.1 © 1992 S.R. & P.C.\n\
  34. Usage: AddIcon <FileName> [ONLY <FileType>] [FILES] [DIRS] [QUIET] [FLOAT] [OVERRIDE] [ALL]\n\
  35. FileName: the object name you want to add icon to.\n\
  36. ONLY FileType: Add icon only to that type\n\
  37. FILES: add icon to files only.\n\
  38. DIRS: add icon to dirs only.\n\
  39. QUIET: if set, no info printed.\n\
  40. FLOAT: UnSnapshot icon\n\
  41. OVERRIDE: Override existing icon if present\n\
  42. ALL: enter subdir.\n";
  43.  
  44. LONG SPrintf(UBYTE * buf, UBYTE * fmt,...);
  45.  
  46. FileType WhatIsTags(char *Name, Tag FirstTag,...)
  47. {
  48.     return (WhatIsBase) ? WhatIs(Name, (struct TagItem *) & FirstTag) : TYPE_UNSCANNED;
  49. }
  50.  
  51. /* FIB can be null but give it if you alredy have it. */
  52. BOOL AddIcon(char *FileName, FileType OnlyType, BOOL Quiet, BOOL Float, BOOL Override, struct DiskObject ** LastDOb, FileType * LastType, struct FileInfoBlock * FIB)
  53. {
  54.     char *IconName = NULL;
  55.     char Buf[255];
  56.     FileType Type;
  57.     LONG WBType = 0;
  58.     BOOL Idem;
  59.     BOOL Ok = FALSE;    /* assume the worst */
  60.     struct DiskObject *dob = NULL;
  61.     BPTR L;
  62.  
  63.     if (!Quiet) {
  64.         SPrintf(Buf, "\"%s\"", FileName);
  65.         Printf("%-32s\t", Buf);
  66.         Flush(Output());
  67.     }
  68.     SPrintf(Buf, "%s.info", FileName);
  69.     if (!Override && (L = Lock(Buf, ACCESS_READ))) {
  70.         UnLock(L);
  71.         if (!Quiet)
  72.             PutStr("Icon already exist.\n");
  73.         return TRUE;
  74.     }
  75.     Type = WhatIsTags(FileName, WI_Deep, DEEPTYPE, (FIB) ? WI_FIB : TAG_IGNORE, FIB, TAG_DONE);
  76.     if (!Quiet) {
  77.         Printf("%-9s ", GetIDString(Type));
  78.         Flush(Output());
  79.     }
  80.     if ((OnlyType != TYPE_UNSCANNED) && CmpFileType(Type, OnlyType)) {
  81.         if (!Quiet)
  82.             PutStr("Type not desired\n");
  83.         return TRUE;
  84.     }
  85.     Idem = !CmpFileType(Type, *LastType);
  86.  
  87.     if (CmpFileType(Type, TYPE_UNKNOWNFILETYPE)) {
  88.         if (!CmpFileType(Type, GetIDType("Volume"))) {
  89.             IconName = "def_Disk";
  90.             FileName = "Disk";
  91.             WBType = WBDISK;    /* To use ROM resident icon if no def_Tool defined in ENV:Sys */
  92.         }
  93.         else if (!CmpFileType(Type, GetIDType("Dir"))) {
  94.             IconName = "def_Drawer";
  95.             WBType = WBDRAWER;    /* To use ROM resident icon if no def_Tool defined in ENV:Sys */
  96.         }
  97.         else {
  98.             IconName = GetIconName(Type);
  99.             if (!StriCmp(IconName, "def_Tool"))
  100.                 WBType = WBTOOL;    /* To use ROM resident icon if no def_Tool defined in ENV:Sys */
  101.             else if (!StriCmp(IconName, "def_Project"))
  102.                 WBType = WBPROJECT;    /* To use ROM resident icon if no def_Project defined in ENV:Sys */
  103.         }
  104.         if (!Quiet) {
  105.             Printf("\"%s\"..", IconName);
  106.             Flush(Output());
  107.         }
  108.         SPrintf(Buf, "Env:Sys/%s", IconName);
  109.         if ((dob = (Idem) ? *LastDOb : ((WBType) ? GetDefDiskObject(WBType) : GetDiskObject(Buf)))) {
  110.             if (Float) {
  111.                 dob->do_CurrentX = NO_ICON_POSITION;
  112.                 dob->do_CurrentY = NO_ICON_POSITION;
  113.             }
  114.             if (PutDiskObject(FileName, dob)) {
  115.                 PutStr(".Ok\n");
  116.                 if (!Idem) {
  117.                     if (*LastDOb)
  118.                         FreeDiskObject(*LastDOb);
  119.                     *LastDOb = dob;    /* memorise the current icon */
  120.                     *LastType = Type;    /* memorise the current type */
  121.                 }
  122.                 Ok = TRUE;
  123.             }
  124.             else
  125.                 PutStr("Can't write icon !\n");
  126.         }
  127.         else
  128.             PutStr("Can't read icon.\n");
  129.     }
  130.     else
  131.         PutStr("Unknown file.\n");
  132.  
  133.     return Ok;
  134. }
  135.  
  136. long main(int argc, char *argv[])
  137. {
  138.     long rc = 0;
  139.     char *FileName;
  140.     BOOL Files, Dirs, Quiet, Float, Override, All;
  141.     char SubDirs[255];
  142.     UBYTE NumSubDir = 0;
  143.  
  144.     FileName = argv[ARG_FileName];
  145.     //StrCat("#?[~(#?.info)]",);
  146.     Files = (BOOL) argv[ARG_FILES];
  147.     Dirs = (BOOL) argv[ARG_DIRS];
  148.     Quiet = (BOOL) argv[ARG_QUIET];
  149.     Float = (BOOL) argv[ARG_FLOAT];
  150.     Override = (BOOL) argv[ARG_OVERRIDE];
  151.     All = (BOOL) argv[ARG_ALL];
  152.     SubDirs[0] = '\0';
  153.     if (!FileName) {
  154.         Printf("%s", CliHelp);
  155.         return 5;
  156.     }
  157.  
  158.     if (WhatIsBase = (struct WhatIsBase *) OpenLibrary("whatis.library", 0l)) {
  159.         if (IconBase = OpenLibrary("icon.library", 37L)) {
  160.             struct DiskObject *LastDOb = NULL;
  161.             FileType LastType = TYPE_UNKNOWNFILETYPE, OnlyType = TYPE_UNSCANNED;
  162.             struct AnchorPath AP;
  163.             LONG ReturnCode = RETURN_ERROR;
  164.             BPTR StartDir;
  165.  
  166.             if (argv[ARG_ONLY])
  167.                 OnlyType = GetIDType(argv[ARG_ONLY]);
  168.             StartDir = CurrentDir(((struct Process *) SysBase->ThisTask)->pr_CurrentDir);
  169.             AP.ap_BreakBits = SIGBREAKF_CTRL_C;    /* Break on these bits    */
  170.             AP.ap_Strlen = 0;
  171.             //MAXFULLNAMELEN;
  172.             AP.ap_Flags = (All) ? APF_DODOT : APF_DODOT | APF_DOWILD;    /* allow convertion of '.' to CurrentDir */
  173.             /*  Well  I have one probleme here:  how corectly handle the
  174.                 ALL  flags:  if the user specify a pattern the MatchNext(
  175.                 hidden)  all  file  and  DIRS  which  don't  match to the
  176.                 pattern,  so  I  don't  see  dir  and  in  fact can't ask
  177.                 MatchNext()  to  enter it.  This limitation is STUPID and
  178.                 important  liumitation.   I  hope is only because I don't
  179.                 know how to do it.  I hope...
  180.             */
  181.             for (ReturnCode = MatchFirst(FileName, &AP);
  182.                  ReturnCode == 0;
  183.                  ReturnCode = MatchNext(&AP)
  184.                 ) {
  185.                 if (AP.ap_Flags & APF_DirChanged) {
  186.                     CurrentDir(AP.ap_Current->an_Lock);
  187.                 }
  188.                 if (AP.ap_Info.fib_DirEntryType > 0) {
  189.                     if (AP.ap_Flags & APF_DIDDIR) {
  190.                         SubDirs[--NumSubDir] = '\0';
  191.                     }
  192.                     else if (All) {
  193.                         if (!Quiet)
  194.                             Printf("\"%s\"\n", AP.ap_Info.fib_FileName);
  195.                         if (!Files) {
  196.                             Printf("%s", SubDirs);
  197.                             AddIcon(AP.ap_Info.fib_FileName, OnlyType, Quiet, Float, Override, &LastDOb, &LastType, &AP.ap_Info);
  198.                         }
  199.                         SubDirs[NumSubDir++] = '/';
  200.                         SubDirs[NumSubDir] = '\0';
  201.  
  202.                         /* make it enter the directory */
  203.                         AP.ap_Flags |= APF_DODIR;
  204.                     }
  205.                     /* clear the completed directory flag */
  206.                     AP.ap_Flags &= ~APF_DIDDIR;
  207.                 }
  208.                 else {
  209.                     /* Here is code for handling each particular file */
  210.                     if (!Dirs && !MatchPatternNoCase("\X80.INFO", AP.ap_Info.fib_FileName)) {
  211.                         Printf("%s", SubDirs);
  212.                         AddIcon(AP.ap_Info.fib_FileName, OnlyType, Quiet, Float, Override, &LastDOb, &LastType, &AP.ap_Info);
  213.                     }
  214.                 }
  215.             }
  216.             MatchEnd(&AP);    /* This absolutely, positively must be called, all of the time. */
  217.             CurrentDir(StartDir);
  218.             if (ReturnCode != ERROR_NO_MORE_ENTRIES) {
  219.                 PrintFault(ReturnCode, NULL);
  220.             }
  221.             if (LastDOb)
  222.                 FreeDiskObject(LastDOb);
  223.  
  224.             CloseLibrary(IconBase);
  225.         }
  226.         else
  227.             Printf("Can't open icon.library !\n");
  228.  
  229.         CloseLibrary((struct Library *) WhatIsBase);
  230.     }
  231.     else
  232.         Printf("Can't open whatis.library !\n");
  233.  
  234.     return rc;
  235. }
  236.